/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.gui;

import java.text.*;
import javax.swing.*;
import javax.swing.text.*;

import blueprint4j.Constants;
import blueprint4j.utils.*;

import java.awt.event.FocusListener;
import java.awt.event.FocusEvent;

public class DataPairFieldString extends DataPairDefault implements FieldBinding,FocusListener {
    
    private BindStringInterface field;
    private JTextField text_field = null;
    private String place_holder = null;
    private MaskFormatter mask = null;
    protected Keyboard keyboard = null;
    private boolean editable = true;
    
    public DataPairFieldString(Binder binder,BindStringInterface p_field) {
        this(binder,p_field, true);
    }
    
    public DataPairFieldString(Binder binder,BindStringInterface p_field, boolean editable) {
        super(binder,p_field);
        this.editable = editable;
        text_field = new JTextField(20);
        if (Keyboard.GUI_VIRTUAL_KEYBOARD && editable) {
            text_field.addFocusListener(this);
        }
        field = p_field;
        setLabel(new JLabel(field.getDescription()));
        setComponent(text_field);
        text_field.setEditable(editable);
    }
    
    public DataPairFieldString(Binder binder,BindStringInterface p_field, String format, String p_place_holder)
    throws ParseException {
        super(binder,p_field);
        place_holder = p_place_holder;
        mask = new MaskFormatter(format);
        mask.setPlaceholder(place_holder);
        text_field = new JFormattedTextField(mask);
        if (Keyboard.GUI_VIRTUAL_KEYBOARD && editable) {
            text_field.addFocusListener(this);
        }
        text_field.setColumns(20);
        field = p_field;
        setLabel(new JLabel(field.getDescription()));
        setComponent(text_field);
    }
    
    public void focusGained(FocusEvent e) {
        if (keyboard == null) {
            if (Gui.getParentFrame(text_field)!= null) {
                keyboard = new Keyboard(Gui.getParentFrame(text_field),field.getDescription());
            } else if (Gui.getParentDialog(text_field)!= null) {
                keyboard = new Keyboard(Gui.getParentDialog(text_field),field.getDescription());
            } else {
                keyboard = new Keyboard();
            }
            keyboard.setText(text_field.getText());
            keyboard.setVisible(true);
            if (keyboard.getText() != null && keyboard.getText().length() > 0) {
                text_field.setText(keyboard.getText());
            }
        } else {
            keyboard = null;
        }
    }
    
    public void focusLost(FocusEvent e) {}
    
    public void setColumns(int col) {
        text_field.setColumns(col);
    }
    
    public boolean isChanged() {
        String f = field.get();
        if (f == null) {
            return (text_field.getText() != null && text_field.getText().length() > 0);
        } else {
            return !f.equals(text_field.getText());
        }
    }
    
    public void intoControl() {
        String string=field.get();
        if (mask != null && string != null) {
            try {
                text_field.setText((String)mask.stringToValue(string));
                text_field.setCaretPosition(0);
                //				text_field.select(0,0);
            } catch (ParseException e) {
                //Log.debug.out(e);
            }
        } else {
            text_field.setText(field.get());
            text_field.setCaretPosition(0);
        }
    }
    
    public void intoBindable() {
        field.set(text_field.getText());
    }
    
    public Bindable getBindable() {
        return field.getBindable();
    }
    
    public BindFieldInterface getBindField() {
        return (BindFieldInterface) field;
    }
    
    public void setBindField(BindFieldInterface p_field) {
        field = (BindStringInterface) p_field;
    }
    
    public void resetControl() {
        text_field.setText("");
    }
}

